package com.cvs.controller;

import com.cvs.bean.JiFen;
import com.cvs.bean.JiFenGood;
import com.cvs.bean.Orders;
import com.cvs.bean.User;
import com.cvs.dao.JiFenMapper;
import com.cvs.service.JiFenGoodService;
import com.cvs.service.JiFenService;
import com.cvs.service.OrdersService;
import com.cvs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jifen")
public class JiFenGoodController {
    @Autowired
    private UserService userService;
    @Autowired
    private JiFenGoodService jiFenGoodService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private JiFenService jiFenService;
    @RequestMapping("/index")
  public String jifenGood(int uid, Model model,int pageNum, int pageSize){
        User user =userService.findOne(uid);
        model.addAttribute("user",user);

        //可以兑换的新产品列表
        //List<JiFenGood> list =jiFenGoodService.findAll();
        model.addAttribute("goods",jiFenGoodService.findAllByPage(pageNum,pageSize));
        return "jifenGood";
  }
  @RequestMapping("/jiFenGoods")
    public @ResponseBody Map jiFenGoods(int uid,int pageNum, int pageSize){
      Map map = new HashMap();
      map.put("status", 200);
      map.put("msg", "查询成功");
      map.put("data", jiFenGoodService.findAllByPage(pageNum,pageSize));
      return map;
  }
    @RequestMapping("/jifenGoodInfo")
    public String jifenGoodInfo(int uid, Model model,int id){
        User user =userService.findOne(uid);
        model.addAttribute("user",user);


        model.addAttribute("goodInfo",jiFenGoodService.findOne(id));
        return "jifenGoodInfo";
    }
    @RequestMapping("/jifenBuy")
    public String jifenBuy(int uid, Model model,int id){
        User user =userService.findUserInfo(uid);
        model.addAttribute("user",user);
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyyMMddHHmmSS");
        model.addAttribute("orderNum",simpleDateFormat1.format(new Date()));
        model.addAttribute("createtime", simpleDateFormat.format(new Date()));
        model.addAttribute("goodInfo",jiFenGoodService.findGoodInfo(id));
        return "jifenBuy";
    }
    @RequestMapping("/makeOrder")
    public String makeOrder(int uid, Model model,int id,String orderNo){
        User user =userService.findOne(uid);
        JiFenGood jiFenGood =jiFenGoodService.findOne(id);
        if(user.getJifen()<jiFenGood.getMoney()){
                return  "fail";
        }
        model.addAttribute("user",user);
        //构建order信息
        Orders orders =new Orders();
        orders.setUid(uid);
        orders.setGid(id);
        orders.setCount(1);
        orders.setOrderno(orderNo);
        orders.setOrdertime(new Date());
        orders.setStatues(1);
        //下单
       int flog= ordersService.insertOrder(orders);
        //扣除积分
        //购买后剩余积分

        int leftjf=user.getJifen()-jiFenGood.getMoney();
        user.setJifen(leftjf);
        userService.updateUser(user);
        //增加积分记录
        JiFen jiFen =new JiFen();
        jiFen.setCreatetime(new Date());
        jiFen.setUid(uid);
        jiFen.setMoney(jiFenGood.getMoney());//设置兑换消耗金额
        jiFenService.addJiFenLog(jiFen);
        //减少可兑换商品
        jiFenGood.setUsecount(jiFenGood.getUsecount()+1);
        jiFenGoodService.updateJiFenGood(jiFenGood);
        model.addAttribute("goodInfo",jiFenGood);
        model.addAttribute("orders",orders);
        return "orders";
    }
}
